package org.csu.onlineedubackend.mapper;

import org.apache.ibatis.annotations.*;
import org.csu.onlineedubackend.entity.Message;

import java.util.List;

@Mapper
public interface MessageMapper {
    
    @Insert("INSERT INTO messages (conversation_id, sender_id, content, created_at, message_type, duration) " +
            "VALUES (#{conversationId}, #{senderId}, #{content}, NOW(), #{messageType}, #{duration})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Message message);
    
    @Select("SELECT * FROM messages WHERE id = #{id}")
    Message findById(Long id);
    
    @Select("SELECT * FROM messages WHERE conversation_id = #{conversationId} ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<Message> findByConversationId(@Param("conversationId") Long conversationId, @Param("offset") int offset, @Param("limit") int limit);
    
    @Select("SELECT * FROM messages WHERE conversation_id = #{conversationId} ORDER BY created_at DESC LIMIT 1")
    Message findLastMessageByConversationId(Long conversationId);

    @Delete("DELETE FROM messages WHERE conversation_id = #{conversationId}")
    int deleteByConversationId(Long conversationId);
} 